﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;

public partial class Admin_UserRoleList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsUserTypeEdit)//违章
                {
                    Bind();
                }
            }

        }
    }

    void Bind()
    {
        AdminBLL bll = new AdminBLL();
        gvUserRole.DataSource = bll.GetAllUserRole();
        gvUserRole.DataBind();
    }

    protected void gvUserRole_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvUserRole.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void gvUserRole_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                int id = Convert.ToInt32(gvUserRole.DataKeys[e.RowIndex].Value);
                AdminBLL bll = new AdminBLL(permission);
                bll.DeleteUserRole(id);

                Response.Redirect("UserRoleList.aspx");
            }
        }
    }
    protected void gvUserRole_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                try
                {
                    switch (e.CommandName)
                    {
                        case "Insert":
                            UserRole d = new UserRole();

                            d.Name = ((TextBox)gvUserRole.FooterRow.FindControl("txtNewName")).Text;
                            
                            AdminBLL bll = new AdminBLL(permission);
                            bll.AddUserRole(d);
                            Response.Redirect("UserRoleList.aspx");
                            break;
                    }
                }
                catch (Exception ex)
                { }
            }
        } 
    }
    protected void gvUserRole_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvUserRole.EditIndex = -1;
        Bind();
    }
    protected void gvUserRole_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                int id = Convert.ToInt32(gvUserRole.DataKeys[e.RowIndex].Value);
                TextBox editName = gvUserRole.Rows[e.RowIndex].FindControl("txtEditName") as TextBox;
                
                UserRole bm = new UserRole();
                bm.ID = id;
                bm.Name = editName.Text;
                
                AdminBLL bll = new AdminBLL(permission);

                bool result = bll.UpdateUserRole(bm);

                Response.Redirect("UserRoleList.aspx");
            }
        }
    }
}